home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.pitt.edu!dsinc!ub!newserve!rebecca!rpi!not-for-mail
- From: tknarr@xmission.com ( Todd Knarr )
- Newsgroups: comp.lang.c++,comp.lang.c++.moderated,comp.lang.c.moderated
- Subject: Re: Q: Rigorous coding using #if !defined(...) and #include
- Date: 3 Feb 1996 09:29:56 -0000
- Organization: Chaos Central
- Sender: cppmods@netlab.cs.rpi.edu
- Approved: kanze@gabi-soft.fr
- Message-ID: <4ev9uk$qr2@netlab.cs.rpi.edu>
- References: <4eo1fs$rr3@netlab.cs.rpi.edu>
- Reply-To: tknarr@xmission.com ( Todd Knarr )
- NNTP-Posting-Host: netlab.cs.rpi.edu
- X-Original-Date: 3 Feb 1996 05:08:47 GMT
-
- In <4eo1fs$rr3@netlab.cs.rpi.edu>, David Carr <davidc@marine.simrad.no> writes:
- >#if !defined (__MYCLASS_H)
- >#define (__MYCLASS_H)
- >
- >#include "Other.h"
- >
- >class MyClass
- >{
- > ...// Uses something in Other.h
- >};
- >
- >#endif
-
- >#if !defined (__OTHER_H)
- >#define (__OTHER_H)
- >
- >#include "MyClass.h"
- >
- >class Other
- >{
- > ...// Uses something in MyClass.h
- >};
- >
- >#endif
-
- >What is the preferred method for writing rigorous code and
- >solving this
- >problem?
- >Any suggestions as to how to make H files a complete interface?
-
- With mutually recursive classes like this, the only solution is to
- take all the code from one class that actually uses members of the
- other and move it out of the .h file and into it's own source file,
- making the class a seperate object module when you're done. The key
- is that, while you have to have all the declarations completely
- available before any use, you don't have to have the definitions
- available. It isn't that VC++ is ignoring #include's in .h files as
- that you can't use members of Other in MyClass until Other's header
- is completely seen, you can't use members of MyClass in Other until
- MyClass's header is completely seen, and one of them has to be seen
- before the other. If you move the member function bodies into seperate
- source files, then the headers contain no mutual recursion and both
- of the headers can be completely seen before any function bodies in
- the class source files. The basic structure is:
-
- Class.h : contains class { }; block with no function bodies.
- Class.cpp : contains only function bodies with no class { }; block.
-
- With your structure, Other.h would #include MyClass.h, MyClass.h would
- #include Other.h, Other.cpp would #include Other.h and MyClass.h, MyClass.cpp
- would #include MyClass.h and Other.h, and you would need to compile
- Other.cpp and MyClass.cpp and link them into the program.
-
- If you want annoying, try dealing with a compile that ignores those
- guard #if !defined( ) lines sometime. It is impossible to write code that
- does mutual #include's, any attempt to do so results in massive error
- spews from perfectly legal code, and the kludges to get around it are
- not pretty.
-
- --
- Todd Knarr : tknarr@xmission.com | finger for PGP public key
- | Member, USENET Cabal
-
- Seriously, I don't want to die just yet. I don't care how
- good-looking they are, I! don't! want! to! die!"
- -- Megazone ( UF1 )
-
-
- [ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
- [ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
- [ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
- [ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
-